package com.formal.product;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class JsonParser {
	private String Upcomming_qury;
	private String Opening_qury;
	private String Playing_qury;
	private String Info_qury;
	private String Search_qury;

	final private String api_key = "xtvntqwgtnkrf4ebq3r5b4tw";

	public JsonParser() {

	}

	public ArrayList<HashMap<String, Object>> getUpComing() {
		ArrayList<HashMap<String, Object>> movies_info = new ArrayList<HashMap<String, Object>>();
		HttpClient httpClient = new DefaultHttpClient();
		try {

			HttpGet httpGet = new HttpGet(
					"http://api.rottentomatoes.com/api/public/v1.0/lists/movies/upcoming.json?apikey="
							+ api_key + "&page_limit=16&page=5");
			HttpResponse response = httpClient.execute(httpGet);

			// HttpEntity entity;
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				Upcomming_qury = EntityUtils.toString(entity);
				JSONObject json_file = new JSONObject(Upcomming_qury);
				JSONArray movies = json_file.getJSONArray("movies");
				int len = (movies != null ? movies.length() : 0);
				for (int i = 0; i < len; i++) {
					JSONObject info_obj = movies.getJSONObject(i);
					String id = info_obj.getString("id");
					String title = info_obj.getString("title");
					String mpaa_rating = info_obj.getString("mpaa_rating");
					String runtime = info_obj.getString("runtime");
					JSONObject rd = info_obj.getJSONObject("release_dates");
					String release_date = rd.getString("theater");
					JSONObject poster = info_obj.getJSONObject("posters");
					String thumbnail = poster.getString("thumbnail");
					Bitmap pic = this.getImage(thumbnail);
					Log.v("TestJson", title);
					Log.v("TestJson", mpaa_rating);
					Log.v("TestJson", runtime);
					Log.v("TestJson", release_date);
					Log.v("TestJson", thumbnail);
					HashMap<String, Object> info = new HashMap<String, Object>();
					// movies_info info = new movies_info();
					info.put("id", id);
					info.put("title", title);
					info.put("mpaa_rating", mpaa_rating);
					info.put("runtime", "Duration: " +runtime +" mins");
					info.put("release_date", "In Threatre: "+release_date);
					info.put("thumbnail", pic);

					movies_info.add(info);
				}
			}
			httpGet.abort();
		} catch (ClientProtocolException e) {
			Log.v("TestJson", "ClientProtocolWrong");
		} catch (IOException ex) {
			Log.v("TestJson", "IOWrong");
		} catch (JSONException ex) {

		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return movies_info;
	}

	public ArrayList<HashMap<String, Object>> getOpening() {
		ArrayList<HashMap<String, Object>> movies_info = new ArrayList<HashMap<String, Object>>();
		HttpClient httpClient_opening = new DefaultHttpClient();
		try {

			HttpGet httpGet = new HttpGet(
					"http://api.rottentomatoes.com/api/public/v1.0/lists/movies/opening.json?apikey="
							+ api_key + "&limit=50");
			HttpResponse response = httpClient_opening.execute(httpGet);

			// HttpEntity entity;
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				Opening_qury = EntityUtils.toString(entity);
				JSONObject json_file = new JSONObject(Opening_qury);
				JSONArray movies = json_file.getJSONArray("movies");
				int len = (movies != null ? movies.length() : 0);
				for (int i = 0; i < len; i++) {
					JSONObject info_obj = movies.getJSONObject(i);
					String id = info_obj.getString("id");
					String title = info_obj.getString("title");
					String mpaa_rating = info_obj.getString("mpaa_rating");
					String runtime = info_obj.getString("runtime");
					JSONObject rd = info_obj.getJSONObject("release_dates");
					String release_date = rd.getString("theater");
					JSONObject poster = info_obj.getJSONObject("posters");
					String thumbnail = poster.getString("thumbnail");
					Bitmap pic = this.getImage(thumbnail);
					Log.v("open", title);
					Log.v("open", mpaa_rating);
					Log.v("open", runtime);
					Log.v("open", release_date);
					Log.v("open", thumbnail);
					HashMap<String, Object> info = new HashMap<String, Object>();
					// movies_info info = new movies_info();
					info.put("id", id);
					info.put("title", title);
					info.put("mpaa_rating", mpaa_rating);
					info.put("runtime", "Duration: " +runtime +" mins");
					info.put("release_date", "In Threatre: "+release_date);
					info.put("thumbnail", pic);

					movies_info.add(info);
				}
			}
			httpGet.abort();
		} catch (ClientProtocolException e) {
			Log.v("TestJson", "ClientProtocolWrong");
		} catch (IOException ex) {
			Log.v("TestJson", "IOWrong");
		} catch (JSONException ex) {
		} finally {
			httpClient_opening.getConnectionManager().shutdown();
		}
		return movies_info;
	}

	public HashMap<String, Object> getMovieInfo(String movie_id) {
		HashMap<String, Object> info = new HashMap<String, Object>();
		HttpClient httpClient_opening = new DefaultHttpClient();
		try {

			HttpGet httpGet = new HttpGet(
					"http://api.rottentomatoes.com/api/public/v1.0/movies/"
							+ movie_id + ".json?apikey=" + api_key);
			HttpResponse response = httpClient_opening.execute(httpGet);
			
			// HttpEntity entity;
			HttpEntity entity = response.getEntity();
//			if(entity == null) {
//				Log.v("open", "len run");
//			}
			if (entity != null) {
				
				Info_qury = EntityUtils.toString(entity);
				Log.v("open", "len run" + Info_qury);
				JSONObject info_obj = new JSONObject(Info_qury);
				//JSONArray movies = json_file.getJSONArray("movies");
				
				//int len = (movies != null ? movies.length() : 0);
				
				//for (int i = 0; i < len; i++) {
					//JSONObject info_obj = movies.getJSONObject(i);
					String id = info_obj.getString("id");
					String title = info_obj.getString("title");
					String mpaa_rating = info_obj.getString("mpaa_rating");
					String runtime = info_obj.getString("runtime");
					String synopsis = info_obj.getString("synopsis");
					JSONObject rd = info_obj.getJSONObject("release_dates");
					String release_date = rd.getString("theater");
					JSONObject poster = info_obj.getJSONObject("posters");
					String profile = poster.getString("profile");
					Bitmap pic = this.getImage(profile);
					Log.v("open", title);
					Log.v("open", mpaa_rating);
					Log.v("open", runtime);
					Log.v("open", release_date);
					Log.v("open", profile);
					Log.v("open", synopsis);

					// movies_info info = new movies_info();
					info.put("id", id);
					info.put("title", title);
					info.put("mpaa_rating", mpaa_rating);
					info.put("runtime", "Duration: " +runtime +" mins");
					info.put("release_date", "In Threatre: "+release_date);
					info.put("thumbnail", pic);
					info.put("synopsis", synopsis);
				}
			//}
			httpGet.abort();
		} catch (ClientProtocolException e) {
			Log.v("TestJson", "ClientProtocolWrong");
		} catch (IOException ex) {
			Log.v("TestJson", "IOWrong");
		} catch (JSONException ex) {
		} finally {
			httpClient_opening.getConnectionManager().shutdown();
		}
		return info;
	}

	public ArrayList<HashMap<String, Object>> getInTheatre() {
		ArrayList<HashMap<String, Object>> movies_info = new ArrayList<HashMap<String, Object>>();
		HttpClient httpClient = new DefaultHttpClient();
		try {

			HttpGet httpGet = new HttpGet(
					"http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?apikey="
							+ api_key + "&page_limit=50&page=5");
			HttpResponse response = httpClient.execute(httpGet);

			// HttpEntity entity;
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				Playing_qury = EntityUtils.toString(entity);
				JSONObject json_file = new JSONObject(Playing_qury);
				JSONArray movies = json_file.getJSONArray("movies");
				int len = (movies != null ? movies.length() : 0);
				for (int i = 0; i < len; i++) {
					JSONObject info_obj = movies.getJSONObject(i);
					String id = info_obj.getString("id");
					String title = info_obj.getString("title");
					String mpaa_rating = info_obj.getString("mpaa_rating");
					String runtime = info_obj.getString("runtime");
					JSONObject rd = info_obj.getJSONObject("release_dates");
					String release_date = rd.getString("theater");
					JSONObject poster = info_obj.getJSONObject("posters");
					String thumbnail = poster.getString("thumbnail");
					Bitmap pic = this.getImage(thumbnail);
					Log.v("TestJson", title);
					Log.v("TestJson", mpaa_rating);
					Log.v("TestJson", runtime);
					Log.v("TestJson", release_date);
					Log.v("TestJson", thumbnail);
					HashMap<String, Object> info = new HashMap<String, Object>();
					// movies_info info = new movies_info();
					info.put("id", id);
					info.put("title", title);
					info.put("mpaa_rating", mpaa_rating);
					info.put("runtime", "Duration: " +runtime +" mins");
					info.put("release_date", "In Threatre: "+release_date);
					info.put("thumbnail", pic);

					movies_info.add(info);
				}
			}
			httpGet.abort();
		} catch (ClientProtocolException e) {
			Log.v("TestJson", "ClientProtocolWrong");
		} catch (IOException ex) {
			Log.v("TestJson", "IOWrong");
		} catch (JSONException ex) {

		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return movies_info;
	}

	public ArrayList<HashMap<String, Object>> SearchMovie(String movie_title) {
		ArrayList<HashMap<String, Object>> movies_info = new ArrayList<HashMap<String, Object>>();
		
		HttpClient httpClient = new DefaultHttpClient();
	
		try {

			HttpGet httpGet = new HttpGet(
					"http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey="
							+ api_key + "&q=" + movie_title + "&page_limit=30");
			Log.v("open", "search run ? ");
			HttpResponse response = httpClient.execute(httpGet);
			
			// HttpEntity entity;
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				Search_qury = EntityUtils.toString(entity);
				JSONObject json_file = new JSONObject(Search_qury);
				JSONArray movies = json_file.getJSONArray("movies");
				int len = (movies != null ? movies.length() : 0);
				for (int i = 0; i < len; i++) {
					JSONObject info_obj = movies.getJSONObject(i);
					String id = info_obj.getString("id");
					String title = info_obj.getString("title");
					String mpaa_rating = info_obj.getString("mpaa_rating");
					String runtime = info_obj.getString("runtime");
					JSONObject rd = info_obj.getJSONObject("release_dates");
					String release_date = rd.getString("theater");
					JSONObject poster = info_obj.getJSONObject("posters");
					String thumbnail = poster.getString("thumbnail");
					Bitmap pic = this.getImage(thumbnail);
					Log.v("TestJson", title);
					Log.v("TestJson", mpaa_rating);
					Log.v("TestJson", runtime);
					Log.v("TestJson", release_date);
					Log.v("TestJson", thumbnail);
					HashMap<String, Object> info = new HashMap<String, Object>();
					// movies_info info = new movies_info();
					info.put("id", id);
					info.put("title", title);
					info.put("mpaa_rating", mpaa_rating);
					info.put("runtime", "Duration: " +runtime +" mins");
					info.put("release_date", "In Threatre: "+release_date);
					info.put("thumbnail", pic);

					movies_info.add(info);
				}
			}
			httpGet.abort();
		} catch (ClientProtocolException e) {
			Log.v("TestJson", "ClientProtocolWrong");
		} catch (IOException ex) {
			Log.v("TestJson", "IOWrong");
		} catch (JSONException ex) {

		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return movies_info;
	}
	
	private Bitmap getImage(String imageLink) {
		if(imageLink==null||imageLink.length()==0)
            return null;
        try{
            URL url=new URL(imageLink);
            URLConnection con=url.openConnection();
            con.connect();
            BufferedInputStream bis=new BufferedInputStream(con.getInputStream());
            Bitmap image=BitmapFactory.decodeStream(bis);
            bis.close();
            return image;
        }catch(IOException e)
        {
            return null;
        }
	}

	
}
